#include "ht1621.h"

uint8_t lcd_table[] = {0xAF,0x06,0x6D,0x4F,0xC6,0xCB,0xEB,0x0E,0xEF,0xCF, // 0~9
						0x00, // " "  0x0A
						0x40, // "-"  0x0B
						0xCB, // "S"  0x0C
						0x61, // "c"  0x0D
						0xEC, // "P"  0x0E
						0xEE, // "A"  0x0F
						0xA1, // "L"  0x10
						0xA8, // "T"  0x11
						0x06, // "I"  0x12
						0xE6, // "H"  0x13
						0x67, // "d"  0x14
						0x63  // "o"  0x15
					};

void LCD_Init(void)
{
	GPIO_Init(GPIOA, LCD_CS|LCD_RD|LCD_WR, GPIO_MODE_OUT_PP_LOW_FAST);
	GPIO_Init(GPIOD, LCD_DA, GPIO_MODE_OUT_PP_LOW_FAST);
	GPIO_WriteHigh(GPIOA, LCD_CS);
	GPIO_WriteHigh(GPIOA, LCD_RD);
	GPIO_WriteHigh(GPIOA, LCD_WR);
	GPIO_WriteHigh(GPIOD, LCD_DA);
	LCD_SendCmd(Sys_en);
	LCD_SendCmd(RCosc);    
	LCD_SendCmd(ComMode);
	LCD_SendCmd(LCD_on);
}

void LCD_SendBit(uint8_t sdata,uint8_t cnt)//送数据程序  
{ 
	//data 的高cnt 位写入HT1621，高位在前
	uint8_t i; 
	for(i=0;i<cnt;i++) 
	{ 
		GPIO_WriteLow(GPIOA, LCD_WR);
		if(sdata&0x80) 
			GPIO_WriteHigh(GPIOD, LCD_DA); 
		else 
			GPIO_WriteLow(GPIOD, LCD_DA);
		GPIO_WriteHigh(GPIOA, LCD_WR);
		sdata<<=1; 
	} 
}

void LCD_SendCmd(uint8_t command) //送命令
{ 
	GPIO_WriteLow(GPIOA, LCD_CS);
	LCD_SendBit(0x80,4);             //写入标志码“100”和9 位command 命令，由于 
	LCD_SendBit(command,8);          //没有使有到更改时钟输出等命令，为了编程方便 
	GPIO_WriteHigh(GPIOA, LCD_CS);   //直接将command 的最高位写“0” 
}

void LCD_ShowNum(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4, uint8_t c)
{
	GPIO_WriteLow(GPIOA, LCD_CS);
	LCD_SendBit(0xA0,3);     //写入标志码“101” 
	LCD_SendBit(0x00,6);     //写入addr 的高6位 

	if (n1==0)
		LCD_SendBit(0x00, 8);
	else
		LCD_SendBit(lcd_table[n1], 8);     // 12

	LCD_SendBit(lcd_table[n2], 8);
	LCD_SendBit(lcd_table[n3], 8);
	LCD_SendBit(lcd_table[n4], 8);

	if (c)
		LCD_SendBit(LCD_COM1|LCD_COM3, 4); // 5
	else
		LCD_SendBit(0x00, 4);

	GPIO_WriteHigh(GPIOA, LCD_CS);
}

void LCD_Refresh(void)
{
	uint8_t n1,n2,n3,n4,c;

	c = 0;
	n1 = 0;

	switch(mode) {
		case MODE_OFFSET_BEGIN  : 
			n2 = LCD_OFFSET_BEGIN>>16;
			n3 = (LCD_OFFSET_BEGIN>>8)%256;
			n4 = LCD_OFFSET_BEGIN%256;
			break;
		case MODE_OFFSET_SET    : 
			if (offset_positive)
				n2 = 0x0A;  // Positive
			else
				n2 = 0x0B;  // Negetive
			n3 = (show_value/10)%10;
			n4 = show_value%10;
			c = 1;
			break;				
		case MODE_PID_BEGIN     : 
			n2 = LCD_PID_BEGIN>>16;
			n3 = (LCD_PID_BEGIN>>8)%256;
			n4 = LCD_PID_BEGIN%256;
			break;
		case MODE_ALERT_BEGIN   : 
			n2 = LCD_ALERT_BEGIN>>16;
			n3 = (LCD_ALERT_BEGIN>>8)%256;
			n4 = LCD_ALERT_BEGIN%256;
			break;
		case MODE_TI_BEGIN      : 
			n2 = LCD_TI_BEGIN>>16;
			n3 = (LCD_TI_BEGIN>>8)%256;
			n4 = LCD_TI_BEGIN%256;
			break;
		case MODE_PWM_BEGIN     : 
			n2 = LCD_PWM_BEGIN>>16;
			n3 = (LCD_PWM_BEGIN>>8)%256;
			n4 = LCD_PWM_BEGIN%256;
			break;
		case MODE_SET_TEMP      : 
			n1 = show_value/1000;
			n2 = (show_value/100)%10;
			n3 = (show_value/10)%10;
			if (bling & (up_cnt<5) & (down_cnt<5))
				n4 = 10; // Dark
			else
				n4 = show_value%10;
			c = 1;
			break;
		case MODE_LOCK_BEGIN    : 
			n2 = LCD_LOCK_BEGIN>>16;
			n3 = (LCD_LOCK_BEGIN>>8)%256;
			n4 = LCD_LOCK_BEGIN%256;
			break;
		case MODE_SHUTDOWN      : 
			n2 = 11; // "-"
			n3 = 11;
			n4 = 11;
			break;
		case MODE_INIT:
			n1=show_value/1000;
			n2=(show_value/100)%10;
			n3=(show_value/10)%10;
			n4=show_value%10;
			c = 1;
			break;
		case MODE_DEFAULT       :
			n2 = LCD_DEFAULT>>16;
			n3 = (LCD_DEFAULT>>8)%256;
			n4 = LCD_DEFAULT%256;
			break;
		case MODE_PID_SET       :
		case MODE_ALERT_SET     :
		case MODE_TI_SET        :
		case MODE_PWM_SET       :
		case MODE_LOCK_SET      :
			n1=show_value/1000;
			n2=(show_value/100)%10;
			n3=(show_value/10)%10;
			if (bling & (up_cnt<5) & (down_cnt<5))
				n4 = 10; // Dark
			else
				n4 = show_value%10;
			break;
	}

	LCD_ShowNum(n1,n2,n3,n4,c);
}
